package com.fang.microcloud.rest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fang.entity.Dept;
import com.fang.microcloud.service.IDeptService;

/*
 * @RestController = @Controller + @ResponseBody
 */
@RestController
public class DeptRest {
	@Resource
	private DiscoveryClient client;

	@Resource
	private IDeptService deptService;
	
	@RequestMapping("/dept/discover")
	public Object discover() {
		return client;
	}

	/*
	 * ymlde security中的配置中sessions:always，保存session状态 session最好是无状态，服务器保存新的状态
	 */
	@RequestMapping("/dept/getSessionId")
	public Object id(HttpServletRequest request) {
		return request.getSession().getId();
	}

	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Object get(@PathVariable("id") long id) {
		return this.deptService.get(id);
	}

	@RequestMapping(value = "/dept/add", method = RequestMethod.GET)
	public Object add(@RequestBody Dept dept) {
		return this.deptService.add(dept);
	}

	@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
	public Object list() {
		return this.deptService.list();
	}

	@RequestMapping("test")
	public void test() {
		ServiceInstance instance = client.getLocalServiceInstance();
		System.out.println(instance.getUri() + "," + instance.getServiceId());
	}
}
